package Core;

import java.io.DataInputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author Наумов Александр
 */
public class Img extends Control
{
    public Image mainImage=null;
	public int bg=0;
	public boolean ispanel;
    
    private String getText(String path)
	{
        DataInputStream dis = new DataInputStream(getClass().getResourceAsStream(path));
        StringBuffer strBuff = new StringBuffer();
        int ch = 0;
        try {
            while ((ch = dis.read()) != -1) {
                strBuff.append((char ) ((ch >= 0xc0 && ch <= 0xFF) ? (ch + 0x350) : ch));
            }
            dis.close();
        } catch (Exception e) {
            System.err.println("ERROR in getText() " + e);
        }
        return strBuff.toString();
	}
   
    public void makeImage(String smart, int w, int h, int gx, int gy, int c, int bg, boolean ispanel)
	{
	    
		Graphics g;

		Image luc=null, ruc=null, ldc=null, rdc=null, lline=null, rline=null, uline=null, dline=null;
		Image si = null;

		int color=c;

		smart="//"+smart;

			try{
				si = Image.createImage(smart);
			}catch(Exception e){System.out.println("Can't find source ("+smart+")");}
			try{
				uline = Image.createImage(si, gx, 0, 1, gy-1, Sprite.TRANS_NONE);
				dline = Image.createImage(si, gx, gy+1, 1, si.getHeight()-gy-1, Sprite.TRANS_NONE);
				lline = Image.createImage(si, 0, gy, gx-1, 1, Sprite.TRANS_NONE);
				rline = Image.createImage(si, gx+1, gy, si.getWidth()-gx-1, 1, Sprite.TRANS_NONE);

				luc = Image.createImage(si, 0, 0, gx-1, gy-1, Sprite.TRANS_NONE);
				rdc = Image.createImage(si, gx+1, gy+1, si.getWidth()-gx-1, si.getHeight()-gy-1, Sprite.TRANS_NONE);
				ldc = Image.createImage(si, 0, gy+1, gx-1, si.getHeight()-gy-1,Sprite.TRANS_NONE);
				ruc = Image.createImage(si, gx+1, 0, si.getWidth()-gx-1, gy-1,Sprite.TRANS_NONE);

				color = c;
			}catch(Exception e){System.out.println("Can't create image");}

		mainImage = Image.createImage(w, h);
		g = mainImage.getGraphics();

		if(ispanel){
            g.setColor(color);
            g.fillRect(0, uline.getHeight(), w+1, h-uline.getHeight()-dline.getHeight()+1);
            for(int i=0; i<w; i++){
                g.drawImage(uline, i, 0, Graphics.LEFT | Graphics.TOP);
                g.drawImage(dline, i, h, Graphics.LEFT | Graphics.BOTTOM);
            }
        }else{
		  g.setColor(bg);
		  g.fillRect(0, 0, w, h);
            g.setColor(color);
            g.fillRect(lline.getWidth(), uline.getHeight()-1, w-lline.getWidth()-rline.getWidth()+1, h-uline.getHeight()-dline.getHeight()+2);
            for(int i=luc.getWidth()-1; i<w-ruc.getWidth()+2; i++)
			{
                g.drawImage(uline, i+1, 0, Graphics.LEFT | Graphics.TOP);
                g.drawImage(dline, i+1, h, Graphics.LEFT | Graphics.BOTTOM);
            }
            for(int i=luc.getHeight()-1; i<h-ldc.getHeight()+2; i++)
			{
                g.drawImage(lline, 0, i, Graphics.LEFT | Graphics.TOP);
                g.drawImage(rline, w, i, Graphics.RIGHT | Graphics.TOP);
            }
            g.drawImage(luc, 0, 0, Graphics.LEFT | Graphics.TOP);
            g.drawImage(ruc, w, 0, Graphics.RIGHT | Graphics.TOP);
            g.drawImage(ldc, 0, h, Graphics.LEFT | Graphics.BOTTOM);
            g.drawImage(rdc, w, h, Graphics.RIGHT | Graphics.BOTTOM);
        }
    }
    
    public int getW()
	{
	    if(mainImage!=null) return mainImage.getWidth();
	    return 0;
    }
    
    public int getH()
	{
	    if(mainImage!=null) return mainImage.getHeight();
	    return 0;
    }
    
    public Img(String smart, int w, int h, int gx, int gy, int c, int bg, boolean panel)
	{
		makeImage(smart, w, h, gx, gy, c, bg, panel);
    }
    
    public Img(Image a)
	{
		mainImage=a;
    }
    
    public Img(String path)
	{
		 try{
			mainImage=Image.createImage(path);
		 }catch(Exception e){
			System.out.println("Unable to open image "+path);
		 }
    }
    
    public Img(String path, int w, int h, int bg, boolean ispanel)
	{
	    
	    String str = getText(path);
	    
	    int i0=0, i=0;
         String s1; //Вспомогательная строка. (исключительно для наглядности)

         i = str.indexOf(",",i0+1); //Ищем символ "," в строке, начиная с i0+1 буквы
         String img=str.substring(i0,i); //Выделяем часть строки с i0 по i-ый символ

         i0=i;
         i = str.indexOf(",",i0+1); 
         s1=str.substring(i0+1,i);
         int gx=(int)Integer.parseInt(s1);
	    
	    i0=i;
         i = str.indexOf(",",i0+1); 
         s1=str.substring(i0+1,i);
         int gy=(int)Integer.parseInt(s1);
	    
	    i0=i;
         i = str.indexOf(";",i0+1); 
         s1=str.substring(i0+1,i);
         int clr=(int)Integer.parseInt(s1, 16);
	    
	    makeImage(img, w, h, gx, gy ,clr, bg, ispanel);

		this.bg = bg;
		this.ispanel = ispanel;
		 name = path;
		 W = mainImage.getWidth();
		 H = mainImage.getHeight();
    }

	public void draw(Graphics g, int x, int y)
	{
		draw(g, x, y, Graphics.LEFT | Graphics.TOP);
	}
    
    public void draw(Graphics g, int x, int y, int s)
	{
		 if(mainImage!=null) g.drawImage(mainImage, x, y, s);
    }

	public int hashCode()
	{
		int hash = 5;
		hash = 29 * hash + (this.mainImage != null ? this.mainImage.hashCode() : 0);
		hash = 29 * hash + this.bg;
		return hash;
	}

	public boolean equals(Object obj)
	{
		Img i;
		if(obj instanceof Img)
		{
			i = (Img)obj;
			if(name.equals(i.name) && getW()==i.getW() && getH()==i.getH() && bg==i.bg) return true;
			else return false;
		}
		else return false;
	}


}    